#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer{
public:
    Computer(const char * brand,double price)
    : _brand(new char[strlen(brand) + 1]())
    , _price(price)
    {
        strcpy(_brand,brand);
        cout << "Computer(const char *,double)" << endl;
    }

    Computer(const Computer & rhs)
    : _brand(new char[strlen(rhs._brand) + 1]())
    , _price(rhs._price)
    {
        strcpy(_brand,rhs._brand);
        /* rhs._price = 100; */
        cout << "Computer(const Computer &)" << endl;
    }

    void print(){
        cout << "brand:" << _brand << endl
            << "price:" << _price << endl;
    }

    ~Computer(){
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
        cout << "~Computer()" << endl;
    }
private:
    char * _brand;
    double _price;
};


void test0(){
    Computer pc("Acer",4500);
    Computer pc2 = pc;
    pc2.print();
    cout <<endl;
    pc.print();
}

void test1(){
    //临时对象
    Computer("HuaWei",5600);
    Computer pc = Computer("Apple",4700);

    int num = 1;//num存在了内存中，可以取地址，称之为左值
    &num;
    int & ref = num;

    //&1;//无法取地址，没有存储在内存中，称之为右值
    /* int & ref2 = 1; //非const引用不能绑定右值 */
    const int & ref2 = 1;//const引用可以绑定右值

    //在使用const引用时，编译器会自动生成一个临时的const对象
    //并将右值绑定到该对象上（延长了生命周期）
    //实现原理右值引用&&
}


int main(void){
    test0();
    return 0;
}
